package txt2bib.bibwriter;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import txt2bib.doctypes.DocType;
import txt2bib.tagtypes.TagType;

public class BibWriter {
	private Collection<DocType> bibEntry;
	private static BibWriter instance = null;
	private static Properties properties;
	private static Integer counter = 0;
	
	static {
		loadConfig();
		
	}
	private BibWriter() {
		bibEntry = new ArrayList<DocType>();
	}
	
	public static BibWriter getInstance() {
		if (instance != null) {
			return instance;
		}
		instance = new BibWriter();
		return instance;
	}
	
	public int size() {
		return bibEntry.size();
	}

	public void add(DocType obj) {
		bibEntry.add(obj);
	}

	public void  writeBibText(String fileName) throws Exception {
		Writer out = null; 
		out = new OutputStreamWriter(new FileOutputStream(fileName, false));
		String string;
		
		// write start file
		for (Iterator<DocType> iterator = bibEntry.iterator(); iterator.hasNext();) {
			DocType obj = iterator.next();
			string = objToString(obj);
			out.write(string);
			out.write("\n");
		}
		
		bibEntry.clear();
		counter = 0;
		// call foreach object a method
		// write end of file

		out.close();
		out = null;
		
	}

	private static String objToString(DocType object) {
		String output = "";
		DocType toScan = object; 
		output = toScan.getStarter() + BibWriterConfig.BIB_ENTITY_OPEN_SEPARATOR;
		if (toScan.getID() == null){
			counter++;
			output += counter.toString();
		}
		else output += toScan.getID();
		
		for (Iterator<TagType>  iterator = toScan.getValues().iterator(); iterator.hasNext();) {
			TagType tag = iterator.next();
			output+=", \n\t" 
				+ tagToString(tag);			
		}		 
		output +=  "\n" + BibWriterConfig.BIB_ENTITY_CLOSE_SEPARATOR;
		return output;
	}
	
	private static String tagToString(TagType tag)
	{
		String ret="";
		ret = tag.getType() +" = " 
		+ BibWriterConfig.BIB_TAG_OPEN_SEPARATOR 
		+ sanitize(tag.getValue()) 
		+ BibWriterConfig.BIB_TAG_CLOSE_SEPARATOR ;
		return ret;
	}

	
	// TODO: TO MOVE
	public static void loadConfig() {
		// Read properties file.
		properties = new Properties();
		try {
			properties.load(new FileInputStream("config.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// TODO: TO MOVE
	public static void storeConfig() {

		// Write properties file.
		try {
			properties.store(new FileOutputStream("config.properties"), null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static String sanitize(String string)
	{
		string = string.replace("\"", "{\"}");		
		string = string.replace("\'", "{'}");
		string = string.trim();
		//string.replace("\"", "\\\"");
		return string;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		for (DocType doc: bibEntry) 
			sb.append((doc == null) ? "Errore\n" : doc.toString());
		
		return sb.toString();
	}

}
